package com.mylove.judas.yaya.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mylove.judas.yaya.R;
import com.mylove.judas.yaya.entity.BaseEntity;
import com.mylove.judas.yaya.mvp.model.ListModel;
import com.mylove.judas.yaya.mvp.presenter.contract.ListContract;
import com.mylove.judas.yaya.mvp.presenter.impl.ListPresenter;
import com.mylove.judas.yaya.ui.adapter.ListAdapter;
import com.mylove.judas.yaya.ui.fragment.base.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZMediaManager;
import cn.jzvd.JZUtils;
import cn.jzvd.JZVideoPlayer;

/**
 * 名人名句
 * Created by Judas on 2017/6/10.
 */

public class MRMJFragment extends BaseFragment<ListPresenter> implements ListContract.ListView{

    private static final String TAG = MRMJFragment.class.getSimpleName();

    private int type = 2;   //文字

    private int pageIndex = 1;
    private int pageSize = 20;

    private View rootView;

    private SmartRefreshLayout srl;
    private RecyclerView rv;

    private ListAdapter adapter;
    private List<BaseEntity> list = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(rootView == null){
            rootView = super.onCreateView(inflater, container, savedInstanceState);
        }

        return rootView;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mrmj;
    }

    @Override
    public void initView() {
        super.initView();
        presenter = new ListPresenter(new ListModel(), this);

        srl = (SmartRefreshLayout) findViewById(R.id.srl);
        rv = (RecyclerView) findViewById(R.id.rv);

        LinearLayoutManager manager = new LinearLayoutManager(mContext);

        rv.setLayoutManager(manager);

        adapter = new ListAdapter(mContext);

        adapter.setList(list);

        rv.setAdapter(adapter);

    }

    public void show(){
        if(pageIndex ==1){
            presenter.getAll(type, pageIndex);
        }
    }

    @Override
    public void setListener() {
        super.setListener();

//        rv.addOnScrollListener(new FeedScrollListener());

        srl.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {//加载
                presenter.getAll(type, pageIndex);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) { //刷新

                list.clear();
                adapter.notifyDataSetChanged();
                pageIndex = 1;

                presenter.getAll(type, pageIndex);

            }
        });


        rv.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                JZVideoPlayer jzvd = (JZVideoPlayer) view.findViewById(R.id.video);
                if (jzvd != null && JZUtils.dataSourceObjectsContainsUri(jzvd.dataSourceObjects, JZMediaManager.getCurrentDataSource())) {
                    JZVideoPlayer.releaseAllVideos();
                }

            }
        });

    }


    @Override
    public void showListResult(List<BaseEntity> list) {
        Log.i(TAG, "========"+list.size());

        pageIndex ++;
        if(srl.isRefreshing()){
            srl.finishRefresh();
        }else if(srl.isLoading()){
            srl.finishLoadmore();

            if(list.size() < pageSize){
                srl.setLoadmoreFinished(true);
            }

        }

        BaseEntity be = new BaseEntity();
        be.setType("00");
        if(list.size() == pageSize){
            list.add(9, be);
            list.add(be);
        }else if(list.size() >= 10){
            list.add(be);
        }

        this.list.addAll(list);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void showNoData() {
        super.showNoData();

        if(srl.isRefreshing()){
            srl.finishRefresh();
        }else if(srl.isLoading()){
            srl.finishLoadmore();
        }

    }


}
